/*
Copyright 2010 Farent-Solutions Ltd.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

 */
package com.farent.solutions.plugins.aribaweb;

import java.io.IOException;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectManager;
import org.netbeans.modules.java.api.common.ant.UpdateImplementation;
import org.netbeans.spi.project.AuxiliaryConfiguration;
import org.netbeans.spi.project.support.ant.AntProjectHelper;
import org.netbeans.spi.project.support.ant.EditableProperties;
import org.openide.util.Mutex;
import org.w3c.dom.Element;

/**
 *
 * @author farouka
 */
public class UpdateProjectImpl implements UpdateImplementation {

    private final Project project;
    private final AntProjectHelper helper;
    private final AuxiliaryConfiguration cfg;
    private Boolean isCurrent;
    private Element cachedElement;

    UpdateProjectImpl(AWNetbeansProject project, AntProjectHelper helper, AuxiliaryConfiguration cfg) {
        assert project != null && helper != null && cfg != null;
        this.project = project;
        this.helper = helper;
        this.cfg = cfg;
    }

    @Override
    public boolean isCurrent() {
        return ProjectManager.mutex().readAccess(new Mutex.Action<Boolean>() {

            @Override
            public Boolean run() {
                synchronized (UpdateProjectImpl.this) {
                    if (isCurrent == null) {
                        if ((cfg.getConfigurationFragment("data", NBawConstants.PROJECT_CONFIGURATION_NAMESPACE, true) != null)) {
                            isCurrent = Boolean.TRUE;
                        } else {
                            isCurrent = Boolean.FALSE;
                        }
                    }
                    return isCurrent;
                }
            }
        }).booleanValue();
    }

    @Override
    public boolean canUpdate() {
        return true;
    }

    @Override
    public void saveUpdate(EditableProperties props) throws IOException {
        this.helper.putPrimaryConfigurationData(getUpdatedSharedConfigurationData(),true);
        ProjectManager.getDefault().saveProject (this.project);
        synchronized(this) {
            this.isCurrent = Boolean.TRUE;
        }
    }

    @Override
    public Element getUpdatedSharedConfigurationData() {
        return cachedElement;
    }

    @Override
    public EditableProperties getUpdatedProjectProperties() {
        return this.helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
    }
}
